package com.kjs.web.controller;

import com.kjs.common.requestModel.*;
import com.kjs.common.resultModel.JsonResult;
import com.kjs.common.resultModel.LoginResult;
import com.kjs.common.tools.*;
import com.kjs.execptionless.BusinessException;
import com.kjs.execptionless.ExceptionCode;
import com.kjs.execptionless.ExceptionMsg;
import com.kjs.web.config.Config;
import com.kjs.web.pojo.Customers;
import com.kjs.web.service.ICustomersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户控制层
 * Created by zmk523@163.com on 2019/6/25 14:17
 */
@Api(value = "UserController", description = "用户相关api")
@CrossOrigin
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private ICustomersService userService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private Config config;

    @ApiOperation(value = "手机密码登录", notes = "手机密码登录", httpMethod = "POST")
    @PostMapping(value = "/loginByTelAndPwd")
    @ResponseBody
    public LoginResult loginByTelAndPwd(@RequestBody(required = false) LoginReq loginReq,
                                        HttpServletRequest request) throws Exception {
        if (StringUtils.isBlank(loginReq.getTelPhone()) || StringUtils.isBlank(loginReq.getPassword())) {
            throw new BusinessException(ExceptionMsg.ParameterError);
        }
        return userService.login(loginReq.getTelPhone(), loginReq.getPassword());
    }

    @ApiOperation(value = "根据ID获取用户信息", notes = "根据ID获取用户信息", httpMethod = "GET")
    @GetMapping(value = "/get")
    @ResponseBody
    public Customers get(@RequestParam("id") Integer id) throws Exception {
        return userService.get(id);
    }

    @ApiOperation(value = "根据ID获取用户token", notes = "根据ID获取用户token", httpMethod = "GET")
    @GetMapping(value = "/getByToken")
    @ResponseBody
    public JsonResult getByToken(@RequestParam("id") Integer id) throws Exception {
        return new JsonResult(ExceptionCode.Normal.value(), "", userService.get(id).getCustomerNo());
    }


    @ApiOperation(value = "根据ID获取用户token", notes = "根据ID获取用户token", httpMethod = "GET")
    @GetMapping(value = "/getInt")
    @ResponseBody
    public Integer getInt(@RequestParam("id") Integer id) throws Exception {
        return 11;
    }


    @ApiOperation(value = "根据ID获取用户token", notes = "根据ID获取用户token", httpMethod = "GET")
    @GetMapping(value = "/getBool")
    @ResponseBody
    public boolean getBool(@RequestParam("id") Integer id) throws Exception {
        return true;
    }

    @ApiOperation(value = "获取数据列表", notes = "获取数据列表", httpMethod = "GET")
    @GetMapping(value = "/get/list")
    @ResponseBody
    public List<LoginResult> getList(@RequestBody(required = false) LoginReq loginReq,
                                     HttpServletRequest request) throws Exception {
        if (StringUtils.isBlank(loginReq.getTelPhone()) || StringUtils.isBlank(loginReq.getPassword())) {
            throw new BusinessException(ExceptionMsg.ParameterError);
        }
        List<LoginResult> list = new ArrayList<>();
        LoginResult loginResult = userService.login(loginReq.getTelPhone(), loginReq.getPassword());
        list.add(loginResult);
        return list;
    }

    @ApiOperation(value = "获取分页数据列表", notes = "获取分页数据列表", httpMethod = "GET")
    @GetMapping(value = "/get/list/page")
    @ResponseBody
    public Map<String, Object> getListToPage(@RequestBody(required = false) LoginReq loginReq,
                                             HttpServletRequest request) throws Exception {
        if (StringUtils.isBlank(loginReq.getTelPhone()) || StringUtils.isBlank(loginReq.getPassword())) {
            throw new BusinessException(ExceptionMsg.ParameterError);
        }
        List<LoginResult> list = new ArrayList<>();
        LoginResult loginResult = userService.login(loginReq.getTelPhone(), loginReq.getPassword());
        list.add(loginResult);
        Map<String, Object> map = new HashMap<>();
        map.put("items", list);
        map.put("count", 111);
        return map;
    }


    @ApiOperation(value = "测试跳转", notes = "测试跳转", httpMethod = "GET")
    @RequestMapping(value = "redirect", method = RequestMethod.GET)
    public void redirectToUrl(HttpServletRequest request,
                              HttpServletResponse response,
                              @RequestParam(value = "returnUrl", required = false, defaultValue = "") String returnUrl) throws Exception {

        response.setHeader("Access-Control-Allow-Origin", "*");
        if (StringUtils.isBlank(returnUrl)) {
            returnUrl = "https://www.baidu.com";
        }
        response.sendRedirect(returnUrl);
    }
}
